package org.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.dk.domain.DataResult;
import org.dk.em.DataResultEnum;
import org.dk.pojo.Cart;
import org.dk.pojo.TbUser;
import org.dk.service.CartService;
import org.dk.util.CookieUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("cartController")
public class CartController {

    @Reference
    private CartService cartService;

    //获取购物车cookie
    @ResponseBody
    @RequestMapping("getCart")
    public List<Cart> getCart(HttpServletRequest request,HttpServletResponse response){
        TbUser tbuser = (TbUser)SecurityUtils.getSubject().getPrincipal();
        String cartlist = CookieUtils.getCookieValue(request, "cartlist", true);
        if(StringUtils.isBlank(cartlist)){
            cartlist="[]";
        }
        List<Cart> carts = JSONObject.parseArray(cartlist, Cart.class);
        if (tbuser == null){
            return carts;
        }else{
            List<Cart> listCartFromRedis = cartService.findListCartFromRedis(tbuser.getUsername());
            //判断cookie是否有数据
            if(carts.size() > 0){
                List<Cart> carts1 = cartService.merageCartListCookieRedis(carts, listCartFromRedis);
                cartService.addCartListToRedis(carts1,tbuser.getUsername());
                //qingkongcookie
                CookieUtils.deleteCookie(request,response,"cartlist");
                return carts1;
            }
            return listCartFromRedis;
        }

    }

    
    @RequestMapping("addCart")
    public String addCarts(HttpServletRequest request, HttpServletResponse response,Long itemId,Integer num){
        //从cookie中获取购物车,cartlist是原来cookie中的购物车
        List<Cart> cartlist = getCart(request,response);
        //调用cookie接口,添加购物车数据
        cartlist = cartService.addCart(cartlist,itemId,num);
        TbUser tbuser = (TbUser)SecurityUtils.getSubject().getPrincipal();
        if (tbuser == null){//未登录保存cookie
            //cookie只能保存字符砖所以需要转换
            String cartlistJson = JSONObject.toJSONString(cartlist);
            //cookieName需要保持一致
            CookieUtils.setCookie(request, response,"cartlist", cartlistJson,true);
        }else{
            cartService.addCartListToRedis(cartlist,tbuser.getUsername());
        }


        return "cart";
    }

    @RequestMapping("selectCart")
    @ResponseBody
    public DataResult selectCart(){
        try {
            TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
            List<Cart> cartListFormRedis = cartService.getCartListFormRedis(user.getUsername());
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(cartListFormRedis);
        }catch (Exception e){
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }

    }
}
